<?php

declare(strict_types=1);

namespace App\Service;

use App\JsonRpc\RpcOrderServiceInterface;
use DtmClient\XA;
use DtmClient\Saga;
use DtmClient\TCC;
use Hyperf\Di\Annotation\Inject;

class OrderService
{
    #[Inject]
    protected RpcOrderServiceInterface $rpcOrderService;
    #[Inject]
    protected XA $xa;
    #[Inject]
    protected Saga $saga;
    #[Inject]
    protected TCC $tcc;
    public function createOrder(): int
    {
        $res = 0;
        // 需要事务 方式如下 不能吞掉异常
        // Dtm 暂不支持 微服务协议

        // $gid = $this->xa->generateGid();
        // $this->xa->globalTransaction($gid, function () {
        //     $this->xa->callBranch(service_api_order() . '/RpcOrderService/createOrder', []);
        //     $this->xa->callBranch(service_api_order() . '/RpcOrderService/createTestError', []);
        // });

        // $gid = $this->saga->generateGid();
        // $this->saga->init($gid);
        // $this->saga->add();
        // $this->saga->submit();

        // $gid = $this->tcc->generateGid();
        // $this->tcc->init();
        // $this->tcc->globalTransaction(function () {
        //     $this->tcc->callBranch();
        //     $this->tcc->callBranch();
        // },$gid);

        // 不需要事务 可以直接调用
        $this->rpcOrderService->createTest();
        $this->rpcOrderService->createTestError();

        return $res;
    }
}
